#ifndef MARKERPARAM_H
#define MARKERPARAM_H

#include "laserparam.h"
#include "wobbleparam.h"
#include "powerrampparam.h"
#include "fly2dparam.h"

/**
 * @brief The MarkerParam class
 * 标刻参数
 */
class MarkerParam
{
public:
    MarkerParam(){}

    MarkerParam toRTCMarkParams(int k)
    {
        MarkerParam markerParam;
        LaserParam& laserParam = markerParam.laserParams();
        WobbleParam& wobbleParam = markerParam.wobbleParams();
        PowerRampParam& powerRamp = markerParam.powerRampParams();
        Fly2DParam& fly2dParam = markerParam.fly2DParam();
        //RTC5中这里是半周期,单位是bit,周期 = 1000000 / Hz * 64;
        uint period = 1000000 / laserParams().frequency() * 64;
        laserParam.setLaserMode(laserParams().laserMode());
        laserParam.setFrequency(laserParams().frequency());
        laserParam.setPower(laserParams().power());
        laserParam.setHalfPeriod(period / 2);
        //RTC5中这里是脉冲时间,单位是bit,时间 = 周期*占空比
        uint length;
        if(laserParams().laserMode() == 0){//如果是二氧化碳模式,则占空比=功率比
            length = (double)laserParams().power() / 100 * period;
        }else{//如果是其它模式,比如使用8bit控制功率,则占空比始终为50%
            length = (double)0.5 * period;
        }
        laserParam.setPulseLength(length);
        //界面速度为mm/s,RTC为bit/ms所有要*k然后/1000;
        laserParam.setMarkSpeed(laserParams().markSpeed() * k / 1000);
        laserParam.setJumpSpeed(laserParams().jumpSpeed() * k / 1000);
        laserParam.setJumpDelay(laserParams().jumpDelay() / 10);//1bit=10us;
        laserParam.setMarkDelay(laserParams().markDelay() / 10);
        laserParam.setPolygonDelay(laserParams().polygonDelay() / 10);
        laserParam.setLaserOnDelay(laserParams().laserOnDelay() * 2);//1bit=0.5us;
        laserParam.setLaserOffDelay(laserParams().laserOffDelay() * 2);

        wobbleParam.setEnabled(wobbleParams().isEnabled());
        wobbleParam.setTransversal(wobbleParams().transversal() * k);
        wobbleParam.setLongitudinal(wobbleParams().longitudinal() * k);
        wobbleParam.setFrequency(wobbleParams().frequency());
        wobbleParam.setMode(wobbleParams().mode());

        powerRamp.setEnabled(powerRampParams().isEnabled());
        powerRamp.setStartLevel(powerRampParams().startLevel());
        powerRamp.setEndLevel(powerRampParams().endLevel());
        powerRamp.setStartLength(powerRampParams().startLength());
        powerRamp.setEndLength(powerRampParams().endLength());

        fly2dParam.setEnable(fly2DParam().isEnable());
        fly2dParam.setCorrable(fly2DParam().isCorrable());
        fly2dParam.setScaleX(fly2DParam().scaleX());
        fly2dParam.setScaleY(fly2DParam().scaleY());
        return markerParam;
    }

    MarkerParam toNv3kMarkParams()
    {
        qDebug() << "................" << "toNv3kMarkParams";
        MarkerParam markerParam;
        LaserParam& laserParam = markerParam.laserParams();
        WobbleParam& wobbleParam = markerParam.wobbleParams();
        PowerRampParam& powerRamp = markerParam.powerRampParams();
        Fly2DParam& fly2dParam = markerParam.fly2DParam();
        //NV3000中这里是周期,单位是ns,周期(us) = 1000000 / Hz;
        uint period = 1000000 / laserParams().frequency() * 1000;
        laserParam.setLaserMode(laserParams().laserMode());
        laserParam.setFrequency(laserParams().frequency());
        laserParam.setPower(laserParams().power());
        laserParam.setHalfPeriod(period);
        //NV3000中这里是高电平持续时间,单位是ns,时间 = 周期*占空比
        uint length;
        if(laserParams().laserMode() == 0){//如果是二氧化碳模式,则占空比=功率比
            length = (double)laserParams().power() / 100 * period;
        }else{//如果是其它模式,比如使用8bit控制功率,则占空比始终为50%
            length = (double)0.5 * period;
        }
        laserParam.setPulseLength(length);
        //速度单位为mm/s,时间单位是us,距离单位为um
        laserParam.setMarkSpeed(laserParams().markSpeed());
        laserParam.setJumpSpeed(laserParams().jumpSpeed());
        laserParam.setJumpDelay(laserParams().jumpDelay());
        laserParam.setMarkDelay(laserParams().markDelay());
        laserParam.setPolygonDelay(laserParams().polygonDelay());
        laserParam.setLaserOnDelay(laserParams().laserOnDelay());
        laserParam.setLaserOffDelay(laserParams().laserOffDelay());

        wobbleParam.setEnabled(wobbleParams().isEnabled());
        wobbleParam.setTransversal(wobbleParams().transversal() * 1000);
        wobbleParam.setLongitudinal(wobbleParams().longitudinal() * 1000);
        wobbleParam.setFrequency(wobbleParams().frequency());
        wobbleParam.setMode(wobbleParams().mode());

        powerRamp.setEnabled(powerRampParams().isEnabled());
        powerRamp.setStartLevel(powerRampParams().startLevel());
        powerRamp.setEndLevel(powerRampParams().endLevel());
        powerRamp.setStartLength(powerRampParams().startLength());
        powerRamp.setEndLength(powerRampParams().endLength());

        fly2dParam.setEnable(fly2DParam().isEnable());
        fly2dParam.setCorrable(fly2DParam().isCorrable());
        fly2dParam.setScaleX(fly2DParam().scaleX());
        fly2dParam.setScaleY(fly2DParam().scaleY());
        return markerParam;
    }

    LaserParam& laserParams() { return mLaserParam; }

    WobbleParam& wobbleParams() { return mWobbleParam; }

    PowerRampParam& powerRampParams() { return mPowerRampParam; }

    Fly2DParam& fly2DParam(){ return mFly2DParam; }
private:
    LaserParam mLaserParam;
    WobbleParam mWobbleParam;
    PowerRampParam mPowerRampParam;
    Fly2DParam mFly2DParam;
};

#endif // MARKERPARAM_H
